package org.argus.job.server.local.persistent.storage.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.annotation.Priority;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.argus.job.server.common.spring.condition.PropertyAndOneBeanCondition;
import org.argus.job.server.extension.dfs.*;
import org.argus.job.server.local.persistent.storage.AbstractDFsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.env.Environment;

import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.DownloadFileRequest;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
 * 使用NFS等工具進行本地存儲的方式
 *
 * @author tjq
 * @since 2023/7/30
 */
@Slf4j
@Priority(value = Integer.MAX_VALUE - 10)
@Conditional(NfsDfsService.NfsDfsCondition.class)
public class NfsDfsService extends AbstractDFsService {
    private static final String TYPE_NFS = "nfs_series";
    public static final String ROOT_PATH = "root-path";


    /**
     * 日志存储 nfs 根目录
     */
    private String nfsRootPath;

    @Override
    public void store(StoreRequest storeRequest) throws IOException {
        String filePath = buildFilePath(storeRequest.getFileLocation());
        FileUtils.copyFile(storeRequest.getLocalFile(), new File(filePath));
    }

    private String buildFilePath(FileLocation fileLocation) throws IOException {
        String folder = String.format("%s/%s", nfsRootPath, fileLocation.getBucket());
        FileUtils.forceMkdir(new File(folder));
        String filePath = String.format("%s/%s", folder, fileLocation.getName());
        return filePath;
    }

    @Override
    public void download(DownloadRequest downloadRequest) throws IOException {
        String filePath = buildFilePath(downloadRequest.getFileLocation());
        FileUtils.copyFile(new File(filePath), downloadRequest.getTarget());
    }

    @Override
    public Optional<FileMeta> fetchFileMeta(FileLocation fileLocation) throws IOException {
        File file = new File(buildFilePath(fileLocation));
        // 文件存在，则返回一个空文件元数据，支持下载
        if (file.exists()) {
            return Optional.of(new FileMeta());
        }

        return Optional.empty();
    }

    @Override
    protected void init(ApplicationContext applicationContext) {
        Environment environment = applicationContext.getEnvironment();
        nfsRootPath = fetchProperty(environment, TYPE_NFS, ROOT_PATH);
        if (nfsRootPath == null) {
            nfsRootPath = "";
        }
    }

    @Override
    public void destroy() throws Exception {

    }

    public static class NfsDfsCondition extends PropertyAndOneBeanCondition {

        @Override
        protected List<String> anyConfigKey() {
            return Lists.newArrayList(buildParameterKey(TYPE_NFS, ROOT_PATH));
        }

        @Override
        protected Class<?> beanType() {
            return DFsService.class;
        }
    }
}
